package com.zbcz.erp.util

import android.app.Activity
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.util.Log
import android.webkit.MimeTypeMap
import java.io.File

fun openDocumentsFilePicker(activity: Activity, requestCode: Int) {
    try {
        val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
        val pdf = "application/pdf"
        val doc = "application/msword"
        val docx = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
        val xls = "application/vnd.ms-excel"
        val xlsx = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        val image = "image/*"
        val mimeTypes = arrayOf(pdf, doc, docx, xls, xlsx, image)
        intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes)
        intent.setType("*/*")
        intent.addCategory(Intent.CATEGORY_OPENABLE)
        activity.startActivityForResult(intent, requestCode)
    } catch (e: ActivityNotFoundException) {
        e.printStackTrace()
    }
}

fun previewFile(context: Context, filePath: String) {
    // 尝试猜测文件的MIME类型，这里使用文件扩展名作为备选方案
    val fileName = File(filePath).name
    val mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
        MimeTypeMap.getFileExtensionFromUrl(fileName).lowercase()
    ) ?: "application/octet-stream" // 未知类型

    // 根据MIME类型预览文件
    when {
        mimeType.startsWith("text/") -> ""
        mimeType.startsWith("image/") -> ""
        // 添加其他MIME类型的处理逻辑
        else -> {
            Log.i("FileUtils", "no file matches")
        }
    }
}
//previewTextFile(context, filePath)
//previewImageFile(context, Uri.fromFile(File(filePath)))
